package com.nbicc.module.interceptor;

import com.nbicc.parent.interceptor.BaseInterceptorRegulation;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by li on 17/12/25.
 * 所有匿名接口设置为全域名跨域
 */
@Component
public class CorsInterceptor extends BaseInterceptorRegulation {

    public CorsInterceptor() {
        this.isenabled = true;
    }

    @Override
    public Set<String> getIncludePatterns() {
        Set<String> includePatterns = new HashSet<String>();
        includePatterns.add("/**");
        return includePatterns;
    }

    @Override
    public Set<String> getExcludePatterns() {
        Set<String> includePatterns = new HashSet<String>();
        return includePatterns;
    }

    /**
     *
     * @param request
     * @param response
     * @param o     @Controller注解下的整个方法名
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object o) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        // 响应类型
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS, DELETE");
        // 响应头设置
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
